﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DBBase;

namespace Business
{
    public partial class Init : Form
    {
        public MainForm m_mainform;
        public Init()
        {
            InitializeComponent();
        }

        public void add(string stable, string sxh, string sname)
        {
            Query objquery = new Query();
            string ssqlex = "";
            ssqlex = string.Format(@"insert into {0} (table_1,table_2) values ('{1}','{2}')", stable, sxh, sname);
            if (!objquery.UpdateTable(ssqlex))
            {
                MessageBox.Show("添加失败");
            }
            m_mainform.saveriji(ssqlex);//保存操作日记
        }

        public void update(string stable, string sid, string sxh, string sname)
        {
            Query objquery = new Query();
            string ssqlex = "";
            ssqlex = string.Format(@"update {0} set table_1='{1}',table_2='{2}' where table_1='{3}'", stable, sxh, sname, sxh);
            if (!objquery.UpdateTable(ssqlex))
            {
                MessageBox.Show("修改失败");
            }
        }

        public void del(string stable, string sxh)
        {
            Query objquery = new Query();
            string ssqlex = "";
            ssqlex = string.Format(@"delete {0} where table_1='{1}'", stable, sxh);
            if (!objquery.UpdateTable(ssqlex))
            {
                MessageBox.Show("删除失败");
            }
        }

        void inicbx(ListView cbx, string dataname)
        {
            cbx.Items.Clear();
            Query objquery = new Query();
            string ssqlex = "";
            ssqlex = string.Format(@"select * from {0}", dataname);
            DataTable dt = objquery.GetTable(ssqlex);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                //cbx.Items.Add(dt.Rows[i]["name"].ToString().Trim());
                //if (i == 0)
                //    cbx.Text = dt.Rows[0]["name"].ToString().Trim();

                ListViewItem li = new ListViewItem();
                li.SubItems[0].Text = string.Format("{0}", i + 1);
                li.SubItems.Add(dt.Rows[i]["table_1"].ToString().Trim());
                li.SubItems.Add(dt.Rows[i]["table_2"].ToString().Trim());
                cbx.Items.Add(li);
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            groupBox1.Text = comboBox1.Text;
            string str = comboBox1.Text;
            switch (str)
            {
                case "车属区域": inicbx(lvcsqy, "CSQY"); break;
                case "号牌种类": inicbx(lvcsqy, "table_hpzl"); break;
                case "变速器形式": inicbx(lvcsqy, "BSQXS"); break;
                case "ABS型号": inicbx(lvcsqy, "table_ABS"); break;
                case "车身颜色": inicbx(lvcsqy, "table_csys"); break;
                case "车辆类型": inicbx(lvcsqy, "table_cllx"); break;
                case "燃油系": inicbx(lvcsqy, "RYX"); break;
                case "燃油种类": inicbx(lvcsqy, "table_rlzl"); break;
                case "使用阶段": inicbx(lvcsqy, "SYJD"); break;
                case "使用性质": inicbx(lvcsqy, "table_syxz"); break;
                case "引车员": inicbx(lvcsqy, "YCY"); break;
                case "底盘类型": inicbx(lvcsqy, "DPLX"); break;
                case "悬架型式": inicbx(lvcsqy, "table_xjxs"); break;
                case "灯制": inicbx(lvcsqy, "table_dz"); break;
                case "管理辖区": inicbx(lvcsqy, "table_glxq"); break;
                case "所属类别": inicbx(lvcsqy, "table_sslb"); break;
                case "检测类别": inicbx(lvcsqy, "table_jclb"); break;
                case "照片代码": inicbx(lvcsqy, "ZPZL"); break;
                case "驱动型式": inicbx(lvcsqy, "table_qdxs"); break;
                case "联网厂家": inicbx(lvcsqy, "table_lwcj"); break;
                case "记账单位": inicbx(lvcsqy, "table_jzdw"); break;
                case "生产单位": inicbx(lvcsqy, "table_scdw"); break;

            }
        }

        private void lvcsqy_SelectedIndexChanged(object sender, EventArgs e)
        {

            int k = lvcsqy.SelectedItems.Count;

            foreach (ListViewItem item in lvcsqy.SelectedItems)
            {
                txtcsqyid.Text = item.SubItems[0].Text.ToString().Trim();
                txtcsqyxh.Text = item.SubItems[1].Text.ToString().Trim();
                txtcsqyname.Text = item.SubItems[2].Text.ToString().Trim();
            }
        }

        private float X;

        private float Y;

        private void setTag(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width + ":" + con.Height + ":" + con.Left + ":" + con.Top + ":" + con.Font.Size;
                if (con.Controls.Count > 0)
                    setTag(con);
            }
        }
        private void setControls(float newx, float newy, Control cons)
        {
            foreach (Control con in cons.Controls)
            {

                string[] mytag = con.Tag.ToString().Split(new char[] { ':' });
                float a = Convert.ToSingle(mytag[0]) * newx;
                con.Width = (int)a;
                a = Convert.ToSingle(mytag[1]) * newy;
                con.Height = (int)(a);
                a = Convert.ToSingle(mytag[2]) * newx;
                con.Left = (int)(a);
                a = Convert.ToSingle(mytag[3]) * newy;
                con.Top = (int)(a);
                Single currentSize = Convert.ToSingle(mytag[4]) * Math.Min(newx, newy);
                if (currentSize <= 0)
                {
                    currentSize = 0.1f;
                }
                con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                if (con.Controls.Count > 0)
                {
                    setControls(newx, newy, con);
                }
            }

        }

        void Form1_Resize(object sender, EventArgs e)
        {
            float newx = (this.Width) / X;
            float newy = this.Height / Y;
            setControls(newx, newy, this);
            this.Text = this.Width.ToString() + " " + this.Height.ToString();

        }
        private void fOtherMatceForm_Load(object sender, EventArgs e)
        {
            this.Resize += new EventHandler(Form1_Resize); this.Resize += new EventHandler(Form1_Resize);

            X = this.Width;
            Y = this.Height;


            setTag(this);
            Form1_Resize(new object(), new EventArgs());//x,y可在实例化时赋值,最后这句是新加的，在MDI时有用

            comboBox1.Items.Add("车身颜色");
            comboBox1.Items.Add("车辆类型");
            comboBox1.Items.Add("ABS型号");
            comboBox1.Items.Add("燃油种类");
            // comboBox1.Items.Add("使用阶段");
            comboBox1.Items.Add("使用性质");
            comboBox1.Items.Add("悬架型式");
            comboBox1.Items.Add("灯制");
            comboBox1.Items.Add("所属类别");
            comboBox1.Items.Add("检测类别");
            comboBox1.Items.Add("驱动型式");
            comboBox1.Items.Add("生产单位");
        }

        private void but_add_Click(object sender, EventArgs e)
        {
            string str = comboBox1.Text;
            switch (str)
            {
                case "车属区域":
                    {
                        add("CSQY", txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "CSQY");
                        break;
                    }
                case "号牌种类":
                    {
                        add("table_hpzl", txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "table_hpzl");
                        break;
                    }
                case "ABS型号":
                    {
                        add("table_ABS", txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "table_ABS");
                        break;
                    }
                case "变速器形式":
                    {
                        add("BSQXS", txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "BSQXS");
                        break;
                    }
                case "车身颜色":
                    {
                        add("table_csys", txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "table_csys");
                        break;
                    }
                case "车辆类型":
                    {
                        add("table_cllx", txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "table_cllx");
                        break;
                    }
                case "燃油系":
                    {
                        add("RYX", txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "RYX");
                        break;
                    }
                case "燃油种类":
                    {
                        add("table_rlzl", txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "table_rlzl");
                        break;
                    }
                case "使用阶段":
                    {
                        add("SYJD", txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "SYJD");
                        break;
                    }
                case "使用性质":
                    {
                        add("table_syxz", txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "table_syxz");
                        break;
                    }
                case "引车员":
                    {
                        add("YCY", txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "YCY");
                        break;
                    }
                case "底盘类型":
                    {
                        add("DPLX", txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "DPLX");
                        break;
                    }
                case "悬架型式":
                    {
                        add("table_xjxs", txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "table_xjxs");
                        break;
                    }
                case "灯制":
                    {
                        add("table_dz", txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "table_dz");
                        break;
                    }
                case "管理辖区":
                    {
                        add("table_glxq", txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "table_glxq");
                        break;
                    }
                case "所属类别":
                    {
                        add("table_sslb", txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "table_sslb");
                        break;
                    }
                case "检测类别":
                    {
                        add("table_jclb", txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "table_jclb");
                        break;
                    }
                case "照片代码":
                    {
                        add("ZPZL", txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "ZPZL");
                        break;
                    }
                case "驱动型式":
                    {
                        add("table_qdxs", txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "table_qdxs");
                        break;
                    }
                case "联网厂家":
                    {
                        add("table_lwcj", txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "table_lwcj");
                        break;
                    }
                case "记账单位":
                    {
                        add("table_jzdw", txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "table_jzdw");
                        break;
                    }
                case "生产单位":
                    {
                        add("table_scdw", txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "table_scdw");
                        break;
                    }

            }
        }

        private void but_config_Click(object sender, EventArgs e)
        {
            string str = comboBox1.Text;
            switch (str)
            {
                case "车属区域":
                    {
                        update("CSQY", txtcsqyid.Text.Trim(), txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "CSQY");
                        break;
                    }
                case "号牌种类":
                    {
                        update("table_hpzl", txtcsqyid.Text.Trim(), txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "table_hpzl");
                        break;
                    }
                case "ABS型号":
                    {
                        update("table_ABS", txtcsqyid.Text.Trim(), txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "table_ABS");
                        break;
                    }
                case "变速器形式":
                    {
                        update("BSQXS", txtcsqyid.Text.Trim(), txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "BSQXS");
                        break;
                    }
                case "车身颜色":
                    {
                        update("table_csys", txtcsqyid.Text.Trim(), txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "table_csys");
                        break;
                    }
                case "车辆类型":
                    {
                        update("table_cllx", txtcsqyid.Text.Trim(), txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "table_cllx");
                        break;
                    }
                case "燃油系":
                    {
                        update("RYX", txtcsqyid.Text.Trim(), txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "RYX");
                        break;
                    }
                case "燃油种类":
                    {
                        update("table_rlzl", txtcsqyid.Text.Trim(), txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "table_rlzl");
                        break;
                    }
                case "使用阶段":
                    {
                        update("SYJD", txtcsqyid.Text.Trim(), txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "SYJD");
                        break;
                    }
                case "使用性质":
                    {
                        update("table_syxz", txtcsqyid.Text.Trim(), txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "table_syxz");
                        break;
                    }
                case "引车员":
                    {
                        update("YCY", txtcsqyid.Text.Trim(), txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "YCY");
                        break;
                    }
                case "底盘类型":
                    {
                        update("DPLX", txtcsqyid.Text.Trim(), txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "DPLX");
                        break;
                    }
                case "悬架型式":
                    {
                        update("table_xjxs", txtcsqyid.Text.Trim(), txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "table_xjxs");
                        break;
                    }
                case "灯制":
                    {
                        update("table_dz", txtcsqyid.Text.Trim(), txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "table_dz");
                        break;
                    }
                case "管理辖区":
                    {
                        update("table_glxq", txtcsqyid.Text.Trim(), txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "table_glxq");
                        break;
                    }
                case "所属类别":
                    {
                        update("table_sslb", txtcsqyid.Text.Trim(), txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "table_sslb");
                        break;
                    }
                case "检测类别":
                    {
                        update("table_jclb", txtcsqyid.Text.Trim(), txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "table_jclb");
                        break;
                    }
                case "照片代码":
                    {
                        update("ZPZL", txtcsqyid.Text.Trim(), txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "ZPZL");
                        break;
                    }
                case "驱动型式":
                    {
                        update("table_qdxs", txtcsqyid.Text.Trim(), txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "table_qdxs");
                        break;
                    }
                case "联网厂家":
                    {
                        update("table_lwcj", txtcsqyid.Text.Trim(), txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "table_lwcj");
                        break;
                    }
                case "记账单位":
                    {
                        update("table_jzdw", txtcsqyid.Text.Trim(), txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "table_jzdw");
                        break;
                    }
                case "生产单位":
                    {
                        update("table_scdw", txtcsqyid.Text.Trim(), txtcsqyxh.Text.Trim(), txtcsqyname.Text.Trim());
                        inicbx(lvcsqy, "table_scdw");
                        break;
                    }
            }
        }

        private void but_delete_Click(object sender, EventArgs e)
        {
            string str = comboBox1.Text;
            switch (str)
            {
                case "车属区域":
                    {
                        del("CSQY", txtcsqyxh.Text.Trim());
                        inicbx(lvcsqy, "CSQY");
                        break;
                    }
                case "号牌种类":
                    {
                        del("table_hpzl", txtcsqyxh.Text.Trim());
                        inicbx(lvcsqy, "table_hpzl");
                        break;
                    }
                case "ABS型号":
                    {
                        del("table_ABS", txtcsqyxh.Text.Trim());
                        inicbx(lvcsqy, "table_ABS");
                        break;
                    }
                case "变速器形式":
                    {
                        del("BSQXS", txtcsqyxh.Text.Trim());
                        inicbx(lvcsqy, "BSQXS");
                        break;
                    }
                case "车身颜色":
                    {
                        del("table_csys", txtcsqyxh.Text.Trim());
                        inicbx(lvcsqy, "table_csys");
                        break;
                    }
                case "车辆类型":
                    {
                        del("table_cllx", txtcsqyxh.Text.Trim());
                        inicbx(lvcsqy, "table_cllx");
                        break;
                    }
                case "燃油系":
                    {
                        del("RYX", txtcsqyxh.Text.Trim());
                        inicbx(lvcsqy, "RYX");
                        break;
                    }
                case "燃油种类":
                    {
                        del("table_rlzl", txtcsqyxh.Text.Trim());
                        inicbx(lvcsqy, "table_rlzl");
                        break;
                    }
                case "使用阶段":
                    {
                        del("SYJD", txtcsqyxh.Text.Trim());
                        inicbx(lvcsqy, "SYJD");
                        break;
                    }
                case "使用性质":
                    {
                        del("table_syxz", txtcsqyxh.Text.Trim());
                        inicbx(lvcsqy, "table_syxz");
                        break;
                    }
                case "引车员":
                    {
                        del("YCY", txtcsqyxh.Text.Trim());
                        inicbx(lvcsqy, "YCY");
                        break;
                    }
                case "底盘类型":
                    {
                        del("DPLX", txtcsqyxh.Text.Trim());
                        inicbx(lvcsqy, "DPLX");
                        break;
                    }
                case "悬架型式":
                    {
                        del("table_xjxs", txtcsqyxh.Text.Trim());
                        inicbx(lvcsqy, "table_xjxs");
                        break;
                    }
                case "灯制":
                    {
                        del("table_dz", txtcsqyxh.Text.Trim());
                        inicbx(lvcsqy, "table_dz");
                        break;
                    }
                case "管理辖区":
                    {
                        del("table_glxq", txtcsqyxh.Text.Trim());
                        inicbx(lvcsqy, "table_glxq");
                        break;
                    }
                case "所属类别":
                    {
                        del("table_sslb", txtcsqyxh.Text.Trim());
                        inicbx(lvcsqy, "table_sslb");
                        break;
                    }
                case "检测类别":
                    {
                        del("table_jclb", txtcsqyxh.Text.Trim());
                        inicbx(lvcsqy, "table_jclb");
                        break;
                    }
                case "照片代码":
                    {
                        del("ZPZL", txtcsqyxh.Text.Trim());
                        inicbx(lvcsqy, "ZPZL");
                        break;
                    }
                case "驱动型式":
                    {
                        del("table_qdxs", txtcsqyxh.Text.Trim());
                        inicbx(lvcsqy, "table_qdxs");
                        break;
                    }
                case "联网厂家":
                    {
                        del("table_lwcj", txtcsqyxh.Text.Trim());
                        inicbx(lvcsqy, "table_lwcj");
                        break;
                    }
                case "记账单位":
                    {
                        del("table_jzdw", txtcsqyxh.Text.Trim());
                        inicbx(lvcsqy, "table_jzdw");
                        break;
                    }
                case "生产单位":
                    {
                        del("table_scdw", txtcsqyxh.Text.Trim());
                        inicbx(lvcsqy, "table_scdw");
                        break;
                    }
            }
        }
    }
}
